package com.calenda.setevent.activity;

import com.calenda.setevent.Define;
import com.calenda.setevent.R;
import com.calenda.setevent.api.BaseAjaxCallBack.IAjaxCallbackMMK;
import com.calenda.setevent.entity.UserEntity;
import com.calenda.setevent.util.PreferenceUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import static com.calenda.setevent.api.SetEventApi.*;

public class LoginActivity extends Activity implements IAjaxCallbackMMK {

	private EditText edtUserName;
	private EditText edtPassword;
	private ImageView btLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity);

		edtUserName = (EditText) this.findViewById(R.id.edtUserName);
		edtPassword = (EditText) this.findViewById(R.id.edtPassword);
		btLogin = (ImageView) this.findViewById(R.id.btLogin);
		btLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String username = edtUserName.getText().toString();
				String password = edtPassword.getText().toString();

				if (!TextUtils.isEmpty(username)
						&& !TextUtils.isEmpty(password)) {
					login(username, password, LoginActivity.this,
							LoginActivity.this);
				} else {
					Toast.makeText(LoginActivity.this,
							"Các trường không được để trống !",
							Toast.LENGTH_LONG).show();
				}
			}
		});

		//

		/*
		 * logout(this,new IAjaxCallbackMMK() {
		 * 
		 * @Override public <T> void handler(T t, int code) { // TODO
		 * Auto-generated method stub if (code == COMPLETE) {
		 * Toast.makeText(LoginActivity.this, "Complete " ,
		 * Toast.LENGTH_LONG).show(); } else if (code == ERROR) { String message
		 * = (String) t; Toast.makeText(LoginActivity.this, "Error " + message,
		 * Toast.LENGTH_LONG).show(); } } });
		 */

		// updateProfile(this, new IAjaxCallbackMMK() {
		//
		// @Override
		// public <T> void handler(T t, int code) {
		// // TODO Auto-generated method stub
		// if (code == COMPLETE) {
		// Toast.makeText(LoginActivity.this, "Complete ",
		// Toast.LENGTH_LONG).show();
		// } else if (code == ERROR) {
		// String message = (String) t;
		// Toast.makeText(LoginActivity.this, "Error " + message,
		// Toast.LENGTH_LONG).show();
		// }
		// }
		// });
	}

	@Override
	public <T> void handler(T t, int code) {
		// TODO Auto-generated method stub
		if (code == COMPLETE) {
			UserEntity userEntity = (UserEntity) t;

			PreferenceUtil.setValue(this, Define.KEY_USER_ENTITY, userEntity
					.toJson().toString());

			startActivity(new Intent(LoginActivity.this, MainActivity.class));

		} else if (code == ERROR) {
			String message = (String) t;
			Toast.makeText(this, "Đăng nhập thất bại: " + message,
					Toast.LENGTH_LONG).show();
		}
	}

}
